package com.chenlei.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.chenlei.service.LoginService;
@Controller
public class LoginControl {
	@Autowired
	LoginService loginService;
	private final static Logger logger = LoggerFactory.getLogger(LoginControl.class);
	@RequestMapping("login")
	public String login(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		if(session.getAttribute("f")!=null){
			System.out.println("hasssssssssssssssss");
		}else{
			session.setAttribute("f", true);
			System.out.println("nulllllllllllllllll");
		}
		logger.info("logg");
		return "login";
	}

	public ModelAndView toSearchPage(MockHttpServletRequest request, MockHttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@RequestMapping(value="dologin",method = RequestMethod.GET)
	public String dologin(HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model) {
		// TODO Auto-generated method stub
		
		if(!StringUtils.isEmpty(request.getParameter("userName"))&&!StringUtils.isEmpty(request.getParameter("passwd"))){
			if(loginService.loginSuccess(request.getParameter("userName"),request.getParameter("passwd"),session)){
				//登陆成功
				return "main";
			}else{
				model.addAttribute("Error", "错误,账号或密码错误");
				return "login";
			}	
		}else{
			model.addAttribute("Error", "错误,账号或密码不能为空");
			return "login";
		}
		
		
	}
	
}
